package com.beiding.dbrequest.common;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 消息传输schema
 */
@Setter
@Getter
@ToString
public class Message implements Serializable {

    //类型请求


    //心跳包
    public static final byte TYPE_HEARTBEAT = 0;

    //请求
    public static final byte TYPE_REQUEST = 1;

    //响应
    public static final byte TYPE_RESPONSE = 2;

    //广播模式(发出去后忽略响应)
    public static final byte TYPE_BROADCAST = 3;

    //状态成功
    public static final int STATUS_SUCCESS = 200;

    //状态出错
    public static final int STATUS_ERROR = 500;

    //调用异常
    public static final int STATUS_INVOKE_ERROR = 503;

    public static final int STATUS_CONNECT_ERROR = 505;

    //未找到
    public static final int STATUS_NOT_FOUND = 404;

    //响应超时
    public static final int STATUS_TIMEOUT = 408;

    //消息类型
    private byte type;

    //请求uri
    private String uri;

    //响应状态
    private Integer status;

    //提示信息
    private String msg;

    //请求id
    private Long requestId;

    //体
    private Serializable body;

    public static Message response(int status,String msg,Serializable body) {
        Message message = new Message();
        message.setType(TYPE_RESPONSE);
        message.setStatus(status);
        message.setMsg(msg);
        message.setBody(body);
        return message;
    }


    public static Message request(String uri,Serializable body) {
        Message message = new Message();
        message.setType(TYPE_REQUEST);
        message.setStatus(STATUS_SUCCESS);
        message.setUri(uri);
        message.setBody(body);
        return message;
    }

    public static Message responseFor(Message requestMessage) {
        Message message = new Message();
        message.setType(TYPE_RESPONSE);
        message.setUri(requestMessage.getUri());
        message.setStatus(STATUS_SUCCESS);
        message.setRequestId(requestMessage.getRequestId());
        return message;
    }

    public Message msg(String msg) {
        this.msg=msg;
        return this;
    }
    public Message status(int status) {
        this.status=status;
        return this;
    }
    public Message requestId(Long requestId) {
        this.requestId=requestId;
        return this;
    }
    public Message body(Serializable body) {
        this.body=body;
        return this;
    }
    public Message type(byte type) {
        this.type=type;
        return this;
    }
    public Message uri(String uri) {
        this.uri=uri;
        return this;
    }

}
